package eclipse.util.modeltools;
import javax.media.opengl.*;
import javax.swing.JFrame;
import eclipse.lib.graphics.*;
import eclipse.lib.graphics.primitives.*;
import eclipse.util.*;
public class ModelDisplay implements GLEventListener
{
	/** Interface between Java and OpenGL */
	public GLInterface 		glInterface;
	/** A new window that is drawn to */
	private JFrame			window;
	/** Creates a MapWindow */
	GLSprite sprite = new GLSprite();
	public ModelDisplay()
	{
		window = new JFrame("Model Viewer");
		window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		window.setSize(640, 480);
		window.setLocation(0, 0);
		window.setVisible(true);
		glInterface = new GLInterface(window, this);
	}
	/** 
	 * Sets the title of the Window.
	 */
	public void setTitle(String title)
	{
		window.setTitle(title);
	}
	@Override
	public void display(GLAutoDrawable GLDraw) 
	{
		GL gl = GLDraw.getGL();			
		gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);
		gl.glLoadIdentity();
		gl.glTranslatef(320f, 240f, 0f);
		//ModelEditor.obj.draw(gl);
		sprite.render(gl);
	}

	@Override
	public void displayChanged(GLAutoDrawable arg0, boolean arg1, boolean arg2) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void init(GLAutoDrawable GLDraw)
	{
		GL gl = GLDraw.getGL(); 			
	    
		gl.glShadeModel(GL.GL_SMOOTH);
		gl.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
		gl.glClearDepth(1.0f);
		gl.glEnable(GL.GL_DEPTH_TEST);
		gl.glEnable(GL.GL_TEXTURE_2D);
				
		gl.glDepthFunc(GL.GL_LEQUAL);		
		gl.glHint(GL.GL_PERSPECTIVE_CORRECTION_HINT, GL.GL_NICEST);
		gl.glHint(GL.GL_POLYGON_SMOOTH_HINT, GL.GL_NICEST);	
		sprite.setTexture(GLTextureLoader.loadIMG(new ERL("jar://gfx/sprites/sprite0"), true), 4);
		sprite.setSprite(2, 1);
	}

	@Override
	public void reshape(GLAutoDrawable drawable, int x, int y, int width, int height)
	{
		@SuppressWarnings("unused")
		double left, right, bottom, top, zNear, zFar, fov, aspect;
		fov 	= 45d;
		aspect 	= (double) width / (double) height;
		zNear 	= 0.1d;
		zFar  	= 1000.0d;
					
		top 	= Math.tan(fov* Math.PI /360) * zNear;
		bottom 	= -top;
		left 	= aspect * bottom;
		right 	= aspect * top;
		
		GL gl = drawable.getGL();			
		gl.setSwapInterval(1);
		gl.glViewport(0, 0, width, height);
		gl.glMatrixMode(GL.GL_PROJECTION);
		gl.glLoadIdentity();			
		//gl.glFrustum(left, right, bottom, top, zNear, zFar);
		gl.glOrtho(0d, 640d, 480d, 0d, 0d, 1000d);
		gl.glMatrixMode(GL.GL_MODELVIEW);
		gl.glLoadIdentity();
		
	}
	
}
